import { useState, useEffect } from 'react'
import { useStore } from 'stook'
import locale from '@/locales'

const useTheme = () => {
  const [theme, setTheme] = useStore('theme', 'light')

  return { theme, setTheme }
}

const useLanguage = (_lang?: langType) => {

  const [language, setLanguage] = useStore('language', _lang || 'zh-CN')

  const [l, setL] = useState(locale(language))

  useEffect(() => {
    setL(locale(language || 'zh-CN'))
  }, [language])

  return { language, l, setLanguage }
}

type refreshKeyType = 'historyFinish'

const useRefresh = (refreshKey: refreshKeyType) => {

  const [historyFinish, setHistoryFinish] = useStore('historyFinish', false)

  const refreshKeyStatusMap = {
    'historyFinish': historyFinish,
  }

  const refreshFunctionMap = {
    'historyFinish': setHistoryFinish,
  }

  const shouldRefresh = () => {
    refreshFunctionMap[refreshKey](true)
  }

  const rebackRefresh = () => {
    refreshFunctionMap[refreshKey](false)
  }

  return { shouldRefresh, rebackRefresh, refreshKeyStatus: refreshKeyStatusMap[refreshKey] }
}

export {
  useTheme,
  useLanguage,
  useRefresh,
}

type langType = 'zh-CN' | 'en-US' | undefined

